package com.quanyan.ic.parkingLot.http.dao.sqlProvider;

import com.quanyan.ic.parkingLot.http.entity.TbTest;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by zhang on 17/8/21.
 */
public class TestSqlProvider {
    private final static Logger logger = LoggerFactory.getLogger(TestSqlProvider.class);

    /**
     * 通过id查询测试信息
     *
     * @param id
     * @return
     */
    public String queryTestById(final Integer id) {
        String sql = new SQL() {
            {
                SELECT("id", "name", "age", "is_delete as isDelete", "create_time as createTime", "update_time as updateTime");
                FROM("tb_test");
                if (id != null && id != 0) {
                    WHERE("id = #{id}");
                }
                ORDER_BY("update_time");
            }
        }.toString();
        logger.info("【queryTestById】：【{}】", sql);
        return sql;
    }

    /**
     * 插入测试数据
     *
     * @param test
     * @return
     */
    public String insertTest(final TbTest test) {
        String sql = new SQL() {
            {

                INSERT_INTO("tb_test");
                if (test.getName() != null) {
                    VALUES("name", "#{name}");
                }
                if (test.getAge() != null) {
                    VALUES("age", "#{age}");
                }
            }
        }.toString();
        logger.info("【insertTest】：【{}】", sql);
        return sql;
    }
}
